//
//  MXCollocaStyleAndCationVC.m
//  MXの衣柜
//
//  Created by 马霄 on 15/8/13.
//  Copyright (c) 2015年 霄 马. All rights reserved.
//
#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)
#import "MXCollocaStyleAndCationVC.h"
#import "AFNetworkHandler.h"
#import "MXCollocationContentCollectionViewCell.h"
#import "MXCollocationContentModel.h"
#import "MXCollocaCarefulChosenVC.h"
#import "MJRefresh.h"
@interface MXCollocaStyleAndCationVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) NSMutableArray  *ItemArray;

@property (nonatomic, assign) NSInteger pageNum;
@end

@implementation MXCollocaStyleAndCationVC
- (void)dealloc
{
    [_ItemArray release];
    [_collectionView release];
    [_titleString release];
    [_classifyID release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    
    // 全局数组初始化
    self.ItemArray = [NSMutableArray array];
    
    // 添加返回和收藏Button
    {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHTPRO * 64)];
        titleLabel.backgroundColor = [UIColor whiteColor];
        titleLabel.text = @"";
        titleLabel.backgroundColor = [UIColor whiteColor];
        titleLabel.userInteractionEnabled = YES;
        [self.view addSubview:titleLabel];
        
        UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        backButton.frame = CGRectMake(WIDTHPRO * 10, HEIGHTPRO * 29, WIDTHPRO * 30, HEIGHTPRO * 30);
        [backButton setBackgroundImage:[UIImage imageNamed:@"backButton_blue"] forState:(UIControlStateNormal)];
        [backButton addTarget:self action:@selector(backAction) forControlEvents:(UIControlEventTouchUpInside)];
        [titleLabel addSubview:backButton];
        
        UILabel *titleWord = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 - WIDTHPRO * 50, HEIGHTPRO * 24, WIDTHPRO * 100, HEIGHTPRO * 40)];
        titleWord.text = self.titleString;
        titleWord.textAlignment = NSTextAlignmentCenter;
        titleWord.font = [UIFont systemFontOfSize:18];
        [titleWord setTextColor:[UIColor redColor]];
        [titleLabel addSubview:titleWord];
    }
    
    [self creatCollectionView];
    
    self.pageNum = 2;
    
    // 引入数据
    [self GetData];

    // 添加刷新方法
    [self loadingCollectionView];
    
    [self refreshTableView];

    
//    [self creatCollectionView];
    // Do any additional setup after loading the view.
}
- (void)creatCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    flowLayout.itemSize = CGSizeMake(WIDTHPRO * 170, HEIGHTPRO * 300);
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(WIDTHPRO * 10 , HEIGHTPRO * 10 , 0, HEIGHTPRO * 10);
#pragma mark - 根据TopView手动更改
//    flowLayout.headerReferenceSize = CGSizeMake(0, HEIGHT - 64 - 49);
    
    self.collectionView = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, HEIGHTPRO * 64, WIDTH, HEIGHT - HEIGHTPRO * 64) collectionViewLayout:flowLayout] autorelease];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.collectionView];
    
    
    [self.collectionView registerClass:[MXCollocationContentCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
}



- (void)GetData
{
    NSString *Path = [NSString stringWithFormat:@"http://api.nanyibang.com/match-list?classify_id=%@&hkm_sign=0660d65ef8455f4e99d678ba04744a02&kind=%ld&member_id=15243&member_type=member&page=1&random_key=60737&version=1.2.7", self.classifyID, (long)self.kindID];
//    NSLog(@"%@", Path);
    [AFNetworkHandler GETwithAFNByURL:Path completion:^(id result) {
//        NSLog(@"result = %@", result);
        
        NSArray *dataArr = [result objectForKey:@"data"];
        
        for (NSDictionary *dict in dataArr) {
            MXCollocationContentModel *model = [[[MXCollocationContentModel alloc] initWithDic:dict] autorelease];
            [self.ItemArray addObject:model];
        }
        
        [self.collectionView reloadData];
    }];
    
    
}

#pragma mark -- 下拉刷新
-(void)refreshTableView
{
    //    __block MXFindVC *findVC = self;
    [self.collectionView addHeaderWithCallback:^{
        //        [self GetFindData];
        [self.collectionView headerEndRefreshing];
    }];
    //添加下拉刷新头部控件
    
    //    [self.collectionView reloadData];
}

#pragma mark -- 上拉加载

-(void)loadingCollectionView
{
    
    //    __unsafe_unretained typeof(self) vc = self;
    [self.collectionView addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSString *Path = [NSString stringWithFormat:@"http://api.nanyibang.com/match-list?classify_id=%@&hkm_sign=0660d65ef8455f4e99d678ba04744a02&kind=%ld&member_id=15243&member_type=member&page=1&random_key=60737&version=1.2.7", self.classifyID, (long)self.kindID];
//            NSLog(@"%@", Path);
            [AFNetworkHandler GETwithAFNByURL:Path completion:^(id result) {
                //        NSLog(@"result = %@", result);
                
                NSArray *dataArr = [result objectForKey:@"data"];
                
                for (NSDictionary *dict in dataArr) {
                    MXCollocationContentModel *model = [[[MXCollocationContentModel alloc] initWithDic:dict] autorelease];
                    [self.ItemArray addObject:model];
                }
                
            }];
            [self.collectionView reloadData];
            [self.collectionView footerEndRefreshing];
        });
    }];
}

/************************* CollectionView Delegate **************************/
//Collection中的组别个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个组别中元件的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.ItemArray.count;
}
//重用机制

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXCollocationContentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.layer.cornerRadius = 8;
    cell.layer.masksToBounds = YES;
    [cell GetInfo:self.ItemArray[indexPath.row]];
    return cell;
}

#pragma mark - 搭配主页CollectionView的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXCollocaCarefulChosenVC *chosenVC = [[[MXCollocaCarefulChosenVC alloc] init] autorelease];
    chosenVC.careChosenID = [self.ItemArray[indexPath.row] collocation_id];
    [self presentViewController:chosenVC animated:YES completion:^{
    }];
}


- (void)backAction
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
